package com.studentmanage.manage_college.service;

import com.studentmanage.framework.domain.base.College;
import com.studentmanage.framework.domain.base.Specialize;
import com.studentmanage.framework.domain.base.response.CollegeCode;
import com.studentmanage.framework.domain.common.response.CommonCode;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.manage_college.dao.CollegeRepository;
import com.studentmanage.manage_college.dao.SpecializeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SpecializeService {
    @Autowired
    private SpecializeRepository specializeRepository;
    @Autowired
    private CollegeRepository collegeRepository;

    //增加专业
    public ResponseResult addSpecialize(Specialize specialize) {
        if(specialize == null || specialize.getCollegeId() == null){
            return new ResponseResult(CollegeCode.ADD_NULL);
        }
        Specialize specialize1 = specializeRepository.findByName(specialize.getName());
        if(specialize1 != null){
            return new ResponseResult(CollegeCode.SPECIALISEXIST);
        }
        Optional<College> optional = collegeRepository.findById(specialize.getCollegeId());
        if(!optional.isPresent()){
            return new ResponseResult(CollegeCode.COLLEGEISEMPTY);
        }
        specializeRepository.save(specialize);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //根据id查询专业
    public ResponseValue<Specialize> findSpecializeById(String id) {
        Optional<Specialize> optional = specializeRepository.findById(id);
        if(optional.isPresent()){
            Specialize specialize = optional.get();
            return new ResponseValue<Specialize>(true, specialize);
        }
        return new ResponseValue<Specialize>(false, null);
    }
}
